package rbac.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import com.pandawork.core.entity.AbstractEntity;

/**
 * RBAC 权限 实体类.<br>
 * 记录了一个权限可以访问哪些 URL.
 * 
 * @author Huadi
 * @author Zoufengyang
 */

@Entity
@Table(name = "t_rbac_permission")
@NamedQuery(name = "permission.getAllPermissions", query = "from RbacPermission")
public class RbacPermission extends AbstractEntity {

	private static final long serialVersionUID = -2485350405061363924L;

	// ---=== Fields ===--- //
	/** <code>Permission</code> ID. */
	@Id
	@Column(name = "id", nullable = false, length = 11)
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int id;

	/** <code>Permission</code> 名称. */
	@Column(name = "name", nullable = false, length = 255)
	private String permissionName;

	/** 该 <code>Permission</code> 可访问的资源字符串. */
	@Column(name = "resources", nullable = false)
	@Lob
	private String resources;

//	/** 权限类别. */
//	@OneToMany(targetEntity = RbacPermissionType.class, fetch = FetchType.LAZY)
//	@JoinTable(name = "t_permission_type", joinColumns = @JoinColumn(name = "permission_id"), inverseJoinColumns = @JoinColumn(name = "type_id"))
//	private Set<RbacPermissionType> permissionType;

	/** 所有具有该权限 (<code>Permission</code>) 的角色 (<code>RbacRole</code>) 实体. */
	@ManyToMany(targetEntity = RbacRole.class, fetch = FetchType.LAZY, mappedBy = "permissions")
	private Set<RbacRole> rolesBelongsTo;

	// ---=== Methods ===--- //
	public Set<String> extractResources() {
		Set<String> strSet = new HashSet<String>();
		for (String str : resources.split(","))
			strSet.add(str);
		return strSet;
	}

	/** 获得拥有此权限的用户. */
	public Set<RbacUser> getUsersUseThisPermission() {
		Set<RbacUser> userSet = new HashSet<RbacUser>();
		for (RbacRole role : rolesBelongsTo) {
			for (RbacUserGroup ug : role.getUserGroupsBelongsTo()) {
				for (RbacUser user : ug.getUsersBelongsTo()) {
					userSet.add(user);
				}
			}
		}
		return userSet;
	}

	// ---=== Getters and Setters ===--- //
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the permissionName
	 */
	public String getPermissionName() {
		return permissionName;
	}

	/**
	 * @param permissionName
	 *            the permissionName to set
	 */
	public void setPermissionName(String permissionName) {
		this.permissionName = permissionName;
	}

	/**
	 * @return the resources
	 */
	public String getResources() {
		return resources;
	}

	/**
	 * @param resources
	 *            the resources to set
	 */
	public void setResources(String resources) {
		this.resources = resources;
	}

	/**
	 * @return the rolesBelongsTo
	 */
	public Set<RbacRole> getRolesBelongsTo() {
		return rolesBelongsTo;
	}

	/**
	 * @param rolesBelongsTo
	 *            the rolesBelongsTo to set
	 */
	public void setRolesBelongsTo(Set<RbacRole> rolesBelongsTo) {
		this.rolesBelongsTo = rolesBelongsTo;
	}

	// ---=== Override ===--- //
	@Override
	public String toString() {
		return "[id: " + String.valueOf(id) + "][permissionName: "
				+ permissionName + "][resources: " + resources + "]";
	}

//	/**
//	 * @return the permissionType
//	 */
//	public Set<RbacPermissionType> getPermissionType() {
//		return permissionType;
//	}
//
//	/**
//	 * @param permissionType
//	 *            the permissionType to set
//	 */
//	public void setPermissionType(Set<RbacPermissionType> permissionType) {
//		this.permissionType = permissionType;
//	}
}
